package com.rongxun.api.utils;

import android.util.Log;

import com.google.gson.Gson;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * 创建者     庄丰泽
 * 创建时间   2020/5/22 15:31
 * 描述
 * <p>
 * 更新者     $
 * 更新时间   $
 * 更新描述
 */
public class EncryUtils {

    public static String PUBLIC_KEY_FILE = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCHfsq/H1VSF7DBs2/YQnZW6MSBus0p4mvPNa4U1CHPM1xZcaQ0tWUItYu79GgGdRv684UTgPQtNTZOSi5poJnvLSBGG540dDDDCfVKqZ26m8vP5XEF2CZJcmJE9YjA/F6T21vr31BPD75GJDHz5sEgubh09mDPZRr7fu8nDOqoqwIDAQAB";

    public static Map<String, String> deelData(Map<String, String> map) {
        String new_str = "";
        map.put("uuid", randomUUID());

        Gson gson = new Gson();

        String data = gson.toJson(map);

        data = getRandomString(getMinute() % 10) + Base64Utils.encodeToString(data);

        data = data.replaceAll("\r|\n", "");

        int start = (int) Math.floor(Double.parseDouble(DivideNumString(String.valueOf(data.length()), "2")));
        String jq_str = data.substring(0, start);
        String s1 = data.substring(start, data.length());
        if (data.indexOf("=") != -1) {//表明有等于号
            int eq_nu = data.indexOf("=");
            String s2 = data.substring(start, eq_nu);
            String end_str = data.substring(eq_nu, data.length());
            new_str = s2 + jq_str + end_str;
        } else {
            new_str = s1 + jq_str;
        }
        map.clear();
        Log.d("deelData", "new_str=" + new_str);
        map.put("data", RSAUtils.encrypt(PUBLIC_KEY_FILE, new_str));

        return map;
    }

    public static String randomUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    //length用户要求产生字符串的长度
    public static String getRandomString(int length) {
        //        if (length == 0 || length == 1) {
        //            length = 10;
        //        }
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    public static int getMinute() {
        Calendar now = Calendar.getInstance();
        int minute = now.get(Calendar.MINUTE);
        return minute;
    }

    /**
     * 浮点数除法，计算金额使用 a/b
     *
     * @param a
     * @param b
     * @return 返回String类型
     */
    public static String DivideNumString(String a, String b) {
        if (isEmpty(a) || isEmpty(b) || Double.parseDouble(a) == 0 || Double.parseDouble(b) == 0) {
            return "0";
        }
        BigDecimal result = new BigDecimal(a).divide(new BigDecimal(b), 6, BigDecimal.ROUND_HALF_UP);

        return result.toString();
    }

    public static boolean isEmpty(String input) {
        if (input == null || input.trim().length() == 0 || "null".equalsIgnoreCase(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }
}
